#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>// 提供chdir函数
#include<dirent.h>//list
#include<sys/stat.h>//提供mkdir函数
#include<sys/types.h>//提供mkdir函数
#include<string.h>
#include<ftw.h>//find函数里的ftw

void mycd(char path[]){
        int result = chdir(path);
        if(result ==-1)
                 printf( "不存在\n");
        else
                 printf("成功进入\n");
}

void mypwd(){
        char buf[120];
        getcwd(buf, sizeof(buf));
        printf("当前工作目录%s\n",buf);
}

void myexit(){
        exit(0);
}

void mymkdir(){
        char filename[25];
        printf("请输入要创建的目录名\n");
        scanf("%s",filename);
        if(mkdir(filename,0777)==0)
        {        printf("%s创建成功\n",filename);}
        else
	{        printf("%s创建失败\n",filename);}
}

void myrmdir(){
        char filename[25];
        printf("请输入要删除的目录名\n");
        scanf("%s",filename);
        if(rmdir(filename)==0)
        {   	 printf("%s删除成功\n",filename);}
        else
        { 	 printf("%s删除失败\n",filename);}
}

void myrename(char filename1[],char filename2[]){
        if(rename(filename1,filename2)==0)
        {       printf("%s成功修改为%s\n",filename1,filename2);}
        else
        {       printf("修改失败\n");}
}

void mylist(){
        DIR* dirp;
        int count=0;
        struct dirent* ent;
        char path[20];
        scanf("%s",path);
        dirp=opendir(path);//打开
         while((ent = readdir(dirp)) != NULL){
        if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0){}
        else
                printf("%s\n",ent->d_name);
	//printf("%s\n",ent-d_name);
        count++;
        if(count%8==0)
                printf("\n");
        }
	//closedir(dirp);//关闭
}

int fn(const char *file,const struct stat *sb,int flag){
	if(flag==FTW_D){
		printf("%s(目录)\n",file);
	}
	else if(flag==FTW_F){
		printf("%s(文件)\n",file);
	}
	return 0;	
}

void myfind(){
	char filename[25];
	printf("请输入文件名或者目录名\n");
	scanf("%s",filename);
	ftw(filename,fn,500);
}

int filecopy(char src[],char des[]){
	FILE *fp1,*fp2;
	char buf[1024];
	int n;
	fp1=fopen(src,"r");
	fp2=fopen(des,"w");
	while((n=fread(buf,sizeof(char),1024,fp1))>0)
		{fwrite(buf,sizeof(char),n,fp2);
		 printf("复制成功");
		}
	fclose(fp1);
	fclose(fp2);
	return 0;
}

int mycopy(char file[],char folder[]){
	char srcname[256];
	char desname[256];
	strcpy(srcname,file);
	strcpy(desname,folder);
	strcat(desname,"/");
	strcat(desname,srcname);
	return filecopy(file,desname);
}

int main(){
        while(1)
{
	printf("+----------------------------------------------------+\n");
	printf("--Linux环境下模拟实现简单命令解释器\n");
        printf("--mypwd显示当前所在目\n");
        printf("--mylist列出指定目录名中的所有目录及文件\n");
        printf("--mycd改变当前工作目录 \n");
        printf("--mymkdir新建目录\n");
        printf("--myrmdir删除目录\n");
        printf("--myexit退出命令解释程序\n");
        printf("--myrename重命名一个文件或目录\n");
        printf("--myfind查找到的文件的绝对路径\n");
        printf("--mycopy复制一个已存在的文件\n");
	printf("+----------------------------------------------------+\n");
        char na[255];
        printf("请选择要操作的命令\n");
        scanf("%s",na);
      	if (strcmp(na,"mypwd") == 0)
                mypwd();
      	else if (strcmp(na,"mylist") == 0)
                mylist();
      	else  if (strcmp(na,"mycd") == 0)
             {
                char pathname[20];
                printf("请输入要进入的目录\n");
                scanf("%s",pathname);
                mycd(pathname);

             }
       	else if (strcmp(na,"mymkdir") == 0)
             { mymkdir();}
       	else   if (strcmp(na,"myrmdir") == 0)
             { myrmdir();}
       	else if (strcmp(na,"myexit") == 0)
             { myexit();}
      	else if(strcmp(na,"myrename")==0)
             {
               char filename1[20];        
	       char filename2[20];
               printf("请输入要修改的文件名及修改后的文件名(中间用空格隔开)\n");
               scanf("%s %s",filename1,filename2);
               myrename(filename1,filename2);
       	      }
       	else if(strcmp(na,"myfind")==0)
               myfind();        
       	else if(strcmp(na,"mycopy")==0)
              {
	       char file[20],file1[200];
	       printf("请输入要复制的文件名和复制到哪里去\n");
	       scanf("%s %s",file,file1);
	       mycopy(file,file1);
	      }
}
        return 0;
}

